//
//  NSDate+string.m
//  BaseFrame
//
//  Created by 蓝水软件 on 2016/11/19.
//  Copyright © 2016年 xiaojiang. All rights reserved.
//

#import "NSDate+tool.h"

@implementation NSDate (tool)


//date转String
+ (NSString *)StringFromDate:(NSDate *)date dateFormat:(NSString *)stringFormat{
    
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:stringFormat];
    return [formatter stringFromDate:date];
    
}

//sring转date
+ (NSDate *)dateFromString:(NSString *)string dateFormat:(NSString *)stringFormat{
    NSDateFormatter *formatter = [NSDateFormatter new];
    [formatter setDateFormat:stringFormat];
    return [formatter dateFromString:string];

}


+ (int)compareDate:(NSString*)date01 withDate:(NSString*)date02 dateFormat:(NSString *)stringFormat{
    int ci;
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    if (stringFormat.length <= 0) {
        [df setDateFormat:@"yyyy-MM-dd"];
    }else{
        [df setDateFormat:stringFormat];
    }
    
    NSDate *dt1 = [[NSDate alloc] init];
    NSDate *dt2 = [[NSDate alloc] init];
    dt1 = [df dateFromString:date01];
    dt2 = [df dateFromString:date02];
    NSComparisonResult result = [dt1 compare:dt2];
    switch (result)
    {
            //date02比date01大
        case NSOrderedAscending: ci=1; break;
            //date02比date01小
        case NSOrderedDescending: ci=-1; break;
            //date02=date01
        case NSOrderedSame: ci=0; break;
        default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
    }
    return ci;
}

+ (NSString *)todayAddDaysToString:(NSInteger)num{
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
    [dateFormater setDateFormat:@"yyyy-MM-dd"];
    NSDate *nowDate = [NSDate date];
    NSTimeInterval time = 60*60*24;
    NSDate *sevenAgo = [nowDate dateByAddingTimeInterval:+time*num];
    return [dateFormater stringFromDate:sevenAgo];

}

+ (NSDate *)todayAddDaysToDate:(NSInteger)num{
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
    [dateFormater setDateFormat:@"yyyy-MM-dd"];
    NSDate *nowDate = [NSDate date];
    NSTimeInterval time = 60*60*24;
    return  [nowDate dateByAddingTimeInterval:+time*num];
}

@end
